package com.gisuni.sddc.platformManager.service;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.gisuni.sddc.platformManager.dao.ModuleDao;
import com.gisuni.sddc.platformManager.dao.RoleDao;
import com.gisuni.sddc.platformManager.model.ModuleInfo;
import com.gisuni.sddc.platformManager.model.Response;
import com.gisuni.sddc.platformManager.model.RoleInfo;
import com.gisuni.sddc.platformManager.model.ServerInfo;
import com.gisuni.sddc.platformManager.model.Tree;
import com.gisuni.sddc.util.StringUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

@IocBean
public class ModuleService   {

	private Log log = Logs.getLog(ModuleService.class);    
    @Inject
    private ModuleDao moduleDao;
  
    private static final String tableName = "SDDCGISPLATFORM.MODULE_INFO";
 
	public Response queryBaseEntityAll(int from, int to, String name) {
		Response response = new Response();
		response.setSuccess(false);
		String Result=null;
 
		try {
			String result = moduleDao.queryBaseEntityAll(  from,  to,  name);
			int total = moduleDao.queryBaseEntityCount( name);
			response.setSuccess(true);
			response.setResult(result);
			response.setTotal(total);
			return response;
		}catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			response.setMsg("查询失败");
			return response;
		}
		
	}

  
    public ModuleInfo findEntityById(String id) {
      return moduleDao.findEntityById(id);
    }

    
    public List<Tree> findTree() {
        List<Tree> trees = new ArrayList<Tree>();
        String sql = "select ID,MODEL_NAME,MODEL_TYPE,MODEL_INDEX,PARENT_ID,CREATE_TIME,UPDATE_TIME,DESCRIPTION from "+tableName+" order by PARENT_ID,MODEL_INDEX";
        List<ModuleInfo> entityList = moduleDao.findEntity(sql);
/*        Tree root = new Tree();
        root.setId("0");
        root.setText("所有模块");
        trees.add(root);*/
        for (ModuleInfo entity : entityList) {
            Tree tree = new Tree();
            tree.setId(entity.getId());
            tree.setText(entity.getModelName());
            tree.setPid(entity.getParentId());
            trees.add(tree);
        }
        return trees;
    }
    
    

	public Response addEntity(HttpServletRequest request) {
   	 Response res = new Response();
   	 res.setSuccess(false);
		 String name = request.getParameter("name");
		 String type = request.getParameter("type");
		 String parentId = request.getParameter("parentId");
		 String index = request.getParameter("moduleIndex");
		 String  description = request.getParameter("description");
		 //TODO 判断
		 String id = UUID.randomUUID().toString().replaceAll("-", "");
		 ModuleInfo entity = new ModuleInfo();
		 entity.setModelName(name);
		 entity.setModelType(type);
		 entity.setParentId(parentId);
		 entity.setDescription(description);
		 if(!StringUtil.isNullOrEmpty(index)){
			 entity.setModelIndex(Long.parseLong(index));
		 }
		 entity.setCreateTime(new Date());
		 entity.setId(id);
 
		 boolean flag  = moduleDao.addEntity(entity);
		 if(flag){
			 res.setSuccess(true);
			 res.setMsg(id);
		 } 
		 
		return res;
	}
	
  
    public Response updateEntity(HttpServletRequest request) {
      	 Response res = new Response();
       	 res.setSuccess(false);
		 String id = request.getParameter("id");
		 String name = request.getParameter("name");
		 String type = request.getParameter("type");
		 String parentId = request.getParameter("parentId");
		 String index = request.getParameter("moduleIndex");
		 String  description = request.getParameter("description");
		 
		 ModuleInfo entity = new ModuleInfo();
		 entity.setModelName(name);
		 entity.setModelType(type);
		 entity.setParentId(parentId);
		 entity.setDescription(description);
		 if(!StringUtil.isNullOrEmpty(index)){
			 entity.setModelIndex(Long.parseLong(index));
		 }
		 
		 entity.setUpdateTime(new Date());
		 entity.setId(id);
		 
		 
		 boolean flag  = moduleDao.updateEntity(entity);
		 if(flag){
			 res.setSuccess(true);
		 } 
		 
		return res;
    }

	  
    public Response deleteEntityById(String id) {
   	 Response res = new Response();
   	 res.setSuccess(false);
   	 try{
   		 //删除
   		boolean flag = moduleDao.execuleSQL("delete from "+tableName+" where ID='"+id+"'");
   		 //删除 
   		if(flag){
   	    		 res.setSuccess(true);
   		}
    	 return res;
   	 }catch(Exception e){
   		 e.printStackTrace();
   		 log.error(e);
   		 res.setMsg("操作失败");
   		 return res;
   	 }
    }

}
